package com.smart.speaker.config;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.protocol.ConnectionConfigurations;
import com.alibaba.dashscope.utils.Constants;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

@Configuration
public class ModelConfig {

    @Value("${ali.dashscope.api-key}")
    private String apiKey;
    @Value("${ali.dashscope.model}")
    private String model;
    @Value("${ali.dashscope.timbre}")
    private String timbre;
    @Value("${ali.dashscope.chat.pool}")
    private int chatPool;

    @Bean
    public Generation generation() {
        Constants.connectionConfigurations = ConnectionConfigurations.builder()
                .connectTimeout(Duration.ofSeconds(10))
                .writeTimeout(Duration.ofSeconds(10))
                .readTimeout(Duration.ofSeconds(10))
                .responseTimeout(Duration.ofSeconds(10))
                .connectionPoolSize(chatPool)
                .connectionIdleTimeout(Duration.ofSeconds(60))
                .maximumAsyncRequests(chatPool)
                .maximumAsyncRequestsPerHost(chatPool)
                .build();
        return new Generation();
    }

    @PostConstruct
    public void init() {
        Key.apiKey = apiKey;
        Key.model = model;
        Key.timbre = timbre;
    }

    public static class Key {
        public static String apiKey;
        public static String model;
        public static String timbre;
    }
}
